package com.ydd.service.impl;

import com.alibaba.fastjson.JSON;
import com.ydd.constant.ConfigConstant;
import com.ydd.dto.ConfigDTO;
import com.ydd.entity.SysConfig;
import com.ydd.mapper.SysConfigMapper;
import com.ydd.mapper.SysConfigMapperMB;
import com.ydd.result.Result;
import com.ydd.service.ISysConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {


    @Autowired
    private SysConfigMapperMB configMapperMB;

    @Override
    public Result getConfigAll() {

        ConfigAllVO vo = new ConfigAllVO();
        List<String> configGroup = configMapperMB.getTabvalue();
        List<ConfigGroupVO> list = new ArrayList<>();
        configGroup.forEach(s -> {
            ConfigGroupVO configGroupVO = new ConfigGroupVO();
            if (s.equals(ConfigConstant.TAB_VALUE_BASE)) {
                configGroupVO.setKey(s);
                configGroupVO.setName("基础配置");
                List<ConfigGroupVO> ConfigValue = configMapperMB.getConfigValue(s);
                ConfigValue.forEach(configGroupVO1 -> {
                    List<GroupValueVO> data = configMapperMB.getData(configGroupVO1.getName());
                    configGroupVO1.setData(data);
                });
                configGroupVO.setChildren(ConfigValue);
            }
            if (s.equals(ConfigConstant.TAB_VALUE_SYSTEM)) {
                configGroupVO.setKey(s);
                configGroupVO.setName("系统设置");
                List<ConfigGroupVO> ConfigValue = configMapperMB.getConfigValue(s);
                ConfigValue.forEach(configGroupVO1 -> {
                    List<GroupValueVO> data = configMapperMB.getData(configGroupVO1.getName());
                    configGroupVO1.setData(data);
                });
                configGroupVO.setChildren(ConfigValue);
            }
            if (s.equals(ConfigConstant.TAB_VALUE_CUSTOM)) {
                configGroupVO.setKey(s);
                configGroupVO.setName("其他设置");
                List<ConfigGroupVO> ConfigValue = configMapperMB.getConfigValue(s);
                ConfigValue.forEach(configGroupVO1 -> {
                    List<GroupValueVO> data = configMapperMB.getData(configGroupVO1.getName());
                    configGroupVO1.setData(data);
                });
                configGroupVO.setChildren(ConfigValue);
            }
            list.add(configGroupVO);
        });

        List<AllData> allData = new ArrayList<>();
        List<SysConfig> configs = this.list();
        configs.forEach(sysConfig -> {
            AllData data = new AllData();
//            String value = sysConfig.getConfigValue();
//            List<String> array = JSON.parseArray(value, String.class);
            data.setConfigValue(null);
            BeanUtils.copyProperties(sysConfig, data);
            allData.add(data);
        });
        List<SysConfig> sysConfigs = this.list();
        ConfigForm configForm = new ConfigForm();
        sysConfigs.forEach(sysConfig -> {
            if (sysConfig.getAttrKey().equals("web_ico")) {
                configForm.setWebIco(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("system_logo")) {
                configForm.setSystemLogo(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("site_name")) {
                configForm.setSiteName(sysConfig.getAttrValue());

                ConfigRules configRules = new ConfigRules();
                configRules.setSiteName(sysConfig.getValidator());
                vo.setConfigRules(configRules);
            }
            if (sysConfig.getAttrKey().equals("site_link")) {
                configForm.setSiteLink(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("site_keywords")) {
                configForm.setSiteKeywords(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("close_website")) {
                configForm.setCloseWebsite(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("copy_code")) {
                configForm.setCopyCode(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("copy_info")) {
                configForm.setCopyInfo(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("phone")) {
                configForm.setPhone(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("copy_url")) {
                configForm.setCopyUrl(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("black_ip")) {
                configForm.setBlackIp(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("login_title")) {
                configForm.setLoginTitle(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("notic_title")) {
                configForm.setNoticTitle(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("loginreg_bg")) {
                configForm.setLoginregBg(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("web_img")) {
                configForm.setWebImg(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("notic_content")) {
                configForm.setNoticContent(sysConfig.getAttrValue());
            }
            if (sysConfig.getAttrKey().equals("login_bg")) {
                configForm.setLoginBg(sysConfig.getAttrValue());
            }
        });

        vo.setConfigForm(configForm);
        vo.setList(list);
        vo.setAllData(allData);
        return Result.success(vo);
    }

    @Transactional
    @Override
    public Result updateConfigAll(List<ConfigDTO> dtos) {

        dtos.forEach(configDTO -> {
            configMapperMB.update(configDTO);
        });

        return Result.success();
    }
}
